package com.chen.blue.service.system.impl;


import com.chen.blue.dictionary.UserConstants;
import com.chen.blue.domain.model.system.SysDictData;
import com.chen.blue.domain.model.system.SysDictType;
import com.chen.blue.exception.ServiceException;
import com.chen.blue.mapper.system.SysDictDataMapper;
import com.chen.blue.mapper.system.SysDictTypeMapper;
import com.chen.blue.result.DataResult;
import com.chen.blue.service.system.ISysDictTypeService;
import com.chen.blue.utils.PageInfoUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Objects;

/**
 * 字典 业务层处理
 *
 * @author ruoyi
 */
@Service("dictTypeService")
public class ISysDictTypeServiceImpl implements ISysDictTypeService {

    @Autowired
    private SysDictTypeMapper dictTypeMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 项目启动时，初始化字典到缓存
     */
    @PostConstruct
    public void init() {
        loadingDictCache();
    }

    /**
     * 根据条件分页查询字典类型
     *
     * @param dictType 字典类型信息
     * @return 字典类型集合信息
     */
    @Override
    public DataResult<List<SysDictType>> selectDictTypeList(SysDictType dictType) {

        if (Objects.nonNull(dictType.getPageNo()) && Objects.nonNull(dictType.getPageSize())) {
            PageHelper.startPage(dictType.getPageNo(), dictType.getPageSize());
        }

        List<SysDictType> sysDictTypeList = dictTypeMapper.selectDictTypeList(dictType);
        PageInfo<SysDictType> pageInfo = new PageInfo<>(sysDictTypeList);
        PageHelper.clearPage();

        DataResult<List<SysDictType>> dataResult = new DataResult<>();
        dataResult.setData(pageInfo.getList());
        PageInfoUtil.setPageInfo(dataResult, pageInfo);

        return dataResult;

    }

    /**
     * 根据所有字典类型
     *
     * @return 字典类型集合信息
     */
    @Override
    public List<SysDictType> selectDictTypeAll() {
        return dictTypeMapper.selectDictTypeAll();
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {


        return dictDataMapper.selectDictDataByType(dictType);

    }

    /**
     * 根据字典类型ID查询信息
     *
     * @param dictId 字典类型ID
     * @return 字典类型
     */
    @Override
    public SysDictType selectDictTypeById(Long dictId) {
        return dictTypeMapper.selectDictTypeById(dictId);
    }

    /**
     * 根据字典类型查询信息
     *
     * @param dictType 字典类型
     * @return 字典类型
     */
    @Override
    public SysDictType selectDictTypeByType(String dictType) {
        return dictTypeMapper.selectDictTypeByType(dictType);
    }

    /**
     * 批量删除字典类型信息
     *
     * @param dictIds 需要删除的字典ID
     */
    @Override
    public int deleteDictTypeByIds(Long[] dictIds) {
        for (Long dictId : dictIds) {
            SysDictType dictType = selectDictTypeById(dictId);
            return dictTypeMapper.deleteDictTypeById(dictId);
        }

        return 0;
    }

    /**
     * 加载字典缓存数据
     */
    @Override
    public void loadingDictCache() {

    }

    /**
     * 清空字典缓存数据
     */
    @Override
    public void clearDictCache() {

    }

    /**
     * 重置字典缓存数据
     */
    @Override
    public void resetDictCache() {
        clearDictCache();
        loadingDictCache();
    }

    /**
     * 新增保存字典类型信息
     *
     * @param dict 字典类型信息
     * @return 结果
     */
    @Override
    public int insertDictType(SysDictType dict) {
        int row = dictTypeMapper.insertDictType(dict);
        return row;
    }

    /**
     * 修改保存字典类型信息
     *
     * @param dict 字典类型信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateDictType(SysDictType dict) {
        SysDictType oldDict = dictTypeMapper.selectDictTypeById(dict.getDictId());
        dictDataMapper.updateDictDataType(oldDict.getDictType(), dict.getDictType());
        int row = dictTypeMapper.updateDictType(dict);
        return row;
    }

    /**
     * 校验字典类型称是否唯一
     *
     * @param dict 字典类型
     * @return 结果
     */
    @Override
    public boolean checkDictTypeUnique(SysDictType dict) {
        Long dictId = Objects.isNull(dict.getDictId()) ? -1L : dict.getDictId();
        SysDictType dictType = dictTypeMapper.checkDictTypeUnique(dict.getDictType());
        if (Objects.nonNull(dictType) && dictType.getDictId().longValue() != dictId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
